<?php
/**
 * Created by PhpStorm.
 * User: adophper <hello@adophper.com>
 * Date: 2017/1/7
 * Time: 11:57
 */
use yii\helpers\Url;
use yii\helpers\Html;

?>
<div class="">
    <div id="jstree"></div>
</div>
<!-- 树勾选项目，多选时用-->
<?= Html::hiddenInput("jsTree_tree_changed_result",'',['id'=>'jsTree_select-tree_changed_result'])?>
<!-- 树加载项目-->
<?= Html::hiddenInput("jsTree_tree_loaded_result",'',['id'=>'jsTree_select-tree_loaded_result'])?>
<!-- 树点选项目，单选时用-->
<?= Html::hiddenInput("jsTree_tree_selected_result",'',['id'=>'jsTree_tree_selected_result'])?>
<script>
    $(function () {
        $('#jstree').on("before.jstree", function (e, data) {
            $('#jsTree_tree_loaded_result').val(data.node.id);
        })
        .jstree({
            'core': {
                "animation": false,
                "multiple": false,
                'data': {
                    'url': '<?=Yii::$app->urlManager->createUrl([Yii::$app->controller->id.'/tree-list'])?>',
                    'data': function (node) {

                    }
                }
            },
            "plugins" : [
                "state"
            ]
        })
        .bind("changed.jstree", function (e, data) {
            $('#jsTree_tree_changed_result').val(data.instance.get_node(data.selected[0]).id);
        })
        .bind("select_node.jstree", function (event, data) {
            var oldid = $('#jsTree_tree_selected_result').val();
            $('#jsTree_tree_selected_result').val(data.instance.get_node(data.selected[0]).id);
            var newid = $('#jsTree_tree_selected_result').val();
            if (oldid != newid) {
                reloadForm(newid);
            }
        }).on("ready.jstree", function (e, data) {
            data.instance.open_all();
        });
    });
</script>